package com.ljx.funchat.user.consume;

import cn.hutool.json.JSONUtil;
import com.ljx.funchat.transaction.config.RabbitMQConfig;
import com.ljx.funchat.user.domain.enums.WSPushTypeEnum;
import com.ljx.funchat.common.domain.dto.PushMessageDTO;
import com.ljx.funchat.websocket.domain.vo.resp.WSBaseResp;
import com.ljx.funchat.websocket.service.WebSocketService;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 推送前端消息消费者
 */
@Component
@RabbitListener(queues = {RabbitMQConfig.PUSH_QUEUE})
public class MsgPushConsumer {

    @Autowired
    private WebSocketService webSocketService;

    @RabbitHandler
    public void pushMessage(String pushMessageMsg) {
        PushMessageDTO pushMessageDTO = JSONUtil.toBean(pushMessageMsg, PushMessageDTO.class);
        // 获取推送类型
        WSPushTypeEnum wsPushTypeEnum = WSPushTypeEnum.of(pushMessageDTO.getPushType());
        switch (wsPushTypeEnum){
            case PERSON:
                pushMessageDTO.getIds().forEach(item -> {
                    webSocketService.sendToUid(item, pushMessageDTO.getWsBaseResp());
                });
                break;
            case ALL:
                // TODO 推送全体
                break;
        }
    }

}
